package main

type node struct {
	Data *RawPacket
	Next *node
}
type PacketList struct {
	Head   *node
	Tail   *node
	EOF    *RawPacket
	length int
}

func (pl *PacketList) Length() int {
	return pl.length
}

type PacketIterator interface {
	HasNext() bool
	Next() *RawPacket
}
type nodeIterator struct {
	cur *node
}

func (ni *nodeIterator) HasNext() bool {
	return ni.cur != nil && ni.cur.Next != nil
}
func (ni *nodeIterator) Next() *RawPacket {
	if ni.cur == nil {
		return nil
	}
	ni.cur = ni.cur.Next
	return ni.cur.Data
}
func (pl *PacketList) Append(rp *RawPacket) {
	pl.length += 1
	if rp.Data()[0] == 0xfe {
		pl.EOF = rp
		return
	}
	n := &node{Data: rp}
	if pl.Head == nil {
		pl.Head = &node{}
		pl.Tail = pl.Head
	}
	pl.Tail.Next = n
	pl.Tail = n
}
func (pl *PacketList) Full() bool {
	return pl.EOF != nil
}
func (pl *PacketList) Iterator() PacketIterator {
	return &nodeIterator{pl.Head}
}
